<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from 'echarts';
import { getUserTwentyFourHoursData } from '@/api/home';
require('echarts/theme/macarons'); // echarts theme
import resize from './mixins/resize';

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null,
      TwentyFourHoursBankCardInformation: [],
      TwentyFourHoursBasicInformation: [],
      TwentyFourHoursContactInformation: [],
      timeCount: [],
      TwentyFourHoursRealNameInformation: [],
      TwentyFourHoursRegCount: [],
      testc: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
      y1: [1, 7, 3, 4, 5, 3, 5],
      y2: [3, 2, 8, 9, 5, 4, 4],
      y3: [4, 9, 3, 4, 5, 6, 2]
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      }
    }
  },
  mounted() {
    // this.getUserTwentyFourHoursData()
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    // 获取用户列表
    async getUserTwentyFourHoursData() {
      const res = await getUserTwentyFourHoursData(this.listQuery);

      this.TwentyFourHoursBankCardInformation =
        res.data.data[4].TwentyFourHoursBankCardInformation;
      this.TwentyFourHoursBasicInformation =
        res.data.data[3].TwentyFourHoursBasicInformation;
      this.TwentyFourHoursContactInformation =
        res.data.data[2].TwentyFourHoursContactInformation;
      this.TwentyFourHoursRealNameInformation =
        res.data.data[1].TwentyFourHoursRealNameInformation;
      this.TwentyFourHoursRegCount = res.data.data[0].TwentyFourHoursRegCount;
      const timeCountTemp = res.data.data[0].dateTime;

      for (var ij = 0; ij < timeCountTemp.length; ij++) {
        this.timeCount[ij] = timeCountTemp[ij].substr(5, 11);
      }
      this.initChart();
    },
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons');
      this.setOptions(this.chartData);
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        xAxis: {
          data: this.timeCount,
          boundaryGap: true, // 不从坐标0开始
          axisTick: {
            show: false
          }
        },
        title: {
          text: '近24小时用户数据统计',
          x: 'center',
          y: 'top',
          padding: [10, 0]
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 90,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          top: '9%',
          x: 'center',
          itemGap: 20,
          data: [
            '完成实名信息',
            '完成联系信息',
            '完成基本信息',
            '完成银行卡信息',
            '注册人数'
          ]
        },
        series: [
          {
            name: '完成银行卡信息',
            itemStyle: {
              normal: {
                color: '#40c9c6',
                lineStyle: {
                  color: '#40c9c6',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: this.TwentyFourHoursBankCardInformation,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: '完成基本信息',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#f4516c',
                lineStyle: {
                  color: '#f4516c',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.TwentyFourHoursBasicInformation,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '完成联系信息',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#6cff00',
                lineStyle: {
                  color: '#6cff00',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.TwentyFourHoursContactInformation,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '完成实名信息',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#36a3f7',
                lineStyle: {
                  color: '#36a3f7',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.TwentyFourHoursRealNameInformation,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          },
          {
            name: '注册人数',
            type: 'bar',
            data: this.TwentyFourHoursRegCount,
            barWidth: '50%',
            itemStyle: {
              normal: {
                color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                  {
                    // 图例使用渐变色
                    offset: 0,
                    color: 'rgb(0, 102, 255)'
                  },
                  {
                    offset: 1,
                    color: 'rgb(0, 153, 255)'
                  }
                ]),
                label: {
                  show: true,
                  position: 'top',
                  textStyle: {
                    fontSize: '15',
                    fontWeight: 'bold',
                    color: 'rgb(51, 51, 51)'
                  }
                }
              }
            }
          }
        ]
      });
    }
  }
};
</script>
